package com.fanhao.dllo.soundfm.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.activity.BaseActivity;
import com.fanhao.dllo.soundfm.adapter.CommentAdapter;
import com.fanhao.dllo.soundfm.entity.CommentResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 16/1/27.
 */
public class CommentActivity extends BaseActivity implements View.OnClickListener {
    private ListView commentLv;
    private CommentAdapter commentAdapter;
    private ImageView backCommentIv;
    private TextView titleCommentTv;
    private String uid;


    @Override
    protected int setLayout() {
        return R.layout.fragment_comments;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        uid = intent.getStringExtra("uid");
        backCommentIv = findView(R.id.ivBack_comment);
        titleCommentTv = findView(R.id.tvTitle_comment);

        backCommentIv.setOnClickListener(this);
        commentLv = findView(R.id.listView_comment);
        commentAdapter = new CommentAdapter(this);
        commentLv.setAdapter(commentAdapter);
        initData();
    }

    @Override
    protected void initData() {
        String url = "http://mobile.ximalaya.com/mobile/track/comment?device=android&trackId=" + uid + "&pageSize=15&pageId=1";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        CommentResult result = gson.fromJson(response,
                                CommentResult.class);
                        if (result != null) {
                        titleCommentTv.setText("所有评论("+result.getTotalCount()+")");
                            List<CommentResult.ListEntity> listEntity = result.getList();
                            if (listEntity != null && listEntity.size() > 0) {
                                commentAdapter.addListData(listEntity);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack_comment:
                finish();
                overridePendingTransition(R.anim.move_in_from_bottom,R.anim.move_out_from_right);
                break;
        }
    }
}
